/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wii.elpro.dao.hb;

import com.wii.elpro.dao.HibernateUtil;
import com.wii.elpro.dao.InqueryDAOInterface;
import com.wii.elpro.entity.Cabang;
import com.wii.elpro.entity.Inquery;
import com.wii.elpro.entity.Project;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author hendro
 */
public class InqueryDAOImpl extends DAOGenericImpl implements InqueryDAOInterface {

    public Inquery getInquery(long id) throws Exception {
        HibernateUtil.beginTransaction();
        Session em = HibernateUtil.getSession();
        try {
            return (Inquery) em.load(Inquery.class, id);
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Inquery> getInqueries() throws Exception {
        HibernateUtil.beginTransaction();
        Session em = HibernateUtil.getSession();
        try {
            return em.createQuery("SELECT p FROM Inquery p").list();
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Inquery> getInqueries(Cabang cab) throws Exception {
        HibernateUtil.beginTransaction();
        Session em = HibernateUtil.getSession();
        try {
            return em.createQuery("SELECT p FROM Inquery p WHERE p.cabang=:input").setParameter("input", cab).list();
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Inquery> getInqueries(Project project) throws Exception {
        HibernateUtil.beginTransaction();
        Session em = HibernateUtil.getSession();
        try {
            return em.createQuery("SELECT p FROM Inquery p WHERE p.project=:input").setParameter("input", project).list();
        } catch (Exception ex) {
            throw ex;
        }
    }

    public Inquery getInquery(String noQuotation) throws Exception {
        HibernateUtil.beginTransaction();
        Session em = HibernateUtil.getSession();
        try {
            return (Inquery) em.createQuery("SELECT p FROM Inquery p WHERE p.noQuotation=:input").setParameter(0, noQuotation).list();
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Inquery> getInqueries(Project project, Cabang cab) throws Exception {
        HibernateUtil.beginTransaction();
        Session em = HibernateUtil.getSession();
        try {
            return em.createQuery("SELECT p FROM Inquery p WHERE p.project=:input1 AND p.cabang=:input2").setParameter("input1", project).setParameter("input2", cab).list();
        } catch (Exception ex) {
            throw ex;
        }
    }

    public Inquery getLastInquiry() throws Exception {
        HibernateUtil.beginTransaction();
        Session em = HibernateUtil.getSession();
        try {
            return (Inquery) em.createQuery("SELECT p FROM Inquery p ORDER BY p.id DESC LIMIT 0,1").setFirstResult(0).setMaxResults(1).uniqueResult();
        } catch (Exception ex) {
            throw ex;
        }
    }
}
